package com.ruoyi.recycle.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.recycle.domain.ExchangeRecord;
import com.ruoyi.recycle.mapper.ExchangeRecordMapper;
import com.ruoyi.recycle.service.ExchangeRecordService;
import com.ruoyi.recycle.vo.ExchangeStatisticsVO;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ExchangeRecordServiceImpl extends ServiceImpl<ExchangeRecordMapper, ExchangeRecord> implements ExchangeRecordService {
    @Override
    public ExchangeStatisticsVO exchangeStatistics(Date begin, Date end) {
        final List<ExchangeRecord> records = list(Wrappers.lambdaQuery(new ExchangeRecord())
                .ge(ExchangeRecord::getCreateTime, begin)
                .le(ExchangeRecord::getCreateTime, end));
        final ExchangeStatisticsVO exchangeStatisticsVO = new ExchangeStatisticsVO();
        if (CollUtil.isNotEmpty(records)) {
            final List<ExchangeRecord> chargeList = records.stream().filter(c -> StrUtil.equals(c.getType(), "充值")).collect(Collectors.toList());
            final List<ExchangeRecord> withdrawList = records.stream().filter(w -> StrUtil.equals(w.getType(), "提现")).collect(Collectors.toList());
            final double totalCharge = chargeList.stream().map(ExchangeRecord::getAmount).mapToDouble(BigDecimal::doubleValue).sum();
            final Map<String, List<ExchangeRecord>> chargeSourceMap = chargeList.stream().collect(Collectors.groupingBy(ExchangeRecord::getSource));
            final double merchantChargeTotal = CollUtil.isEmpty(chargeSourceMap.get("综合站")) ? 0.0 : chargeSourceMap.get("综合站").stream()
                    .map(ExchangeRecord::getAmount)
                    .mapToDouble(BigDecimal::doubleValue)
                    .sum();
            final double clientChargeTotal = CollUtil.isEmpty(chargeSourceMap.get("用户")) ? 0.0 : chargeSourceMap.get("用户").stream()
                    .map(ExchangeRecord::getAmount)
                    .mapToDouble(BigDecimal::doubleValue)
                    .sum();
            final double totalWithdraw = withdrawList.stream().map(ExchangeRecord::getAmount).mapToDouble(BigDecimal::doubleValue).sum();
            final Map<String, List<ExchangeRecord>> withdrawMap = withdrawList.stream().collect(Collectors.groupingBy(ExchangeRecord::getSource));
            final double merchantWithdrawTotal = CollUtil.isEmpty(withdrawMap.get("综合站")) ? 0.0 : withdrawMap.get("综合站").stream()
                    .map(ExchangeRecord::getAmount)
                    .mapToDouble(BigDecimal::doubleValue)
                    .sum();
            final double clientWithdrawTotal = CollUtil.isEmpty(withdrawMap.get("用户")) ? 0.0 : withdrawMap.get("用户").stream()
                    .map(ExchangeRecord::getAmount)
                    .mapToDouble(BigDecimal::doubleValue)
                    .sum();
            final double salesmanWithdrawTotal = CollUtil.isEmpty(withdrawMap.get("业务员")) ? 0.0 : withdrawMap.get("业务员").stream()
                    .map(ExchangeRecord::getAmount)
                    .mapToDouble(BigDecimal::doubleValue)
                    .sum();
            final double flowPlatformWithdrawTotal = CollUtil.isEmpty(withdrawMap.get("引流平台")) ? 0.0 : withdrawMap.get("引流平台").stream()
                    .map(ExchangeRecord::getAmount)
                    .mapToDouble(BigDecimal::doubleValue)
                    .sum();
            return exchangeStatisticsVO
                    .setTotalCharge(totalCharge)
                    .setMerchantChargeTotal(merchantChargeTotal)
                    .setClientChargeTotal(clientChargeTotal)
                    .setTotalWithdraw(totalWithdraw)
                    .setMerchantWithdrawTotal(merchantWithdrawTotal)
                    .setClientWithdrawTotal(clientWithdrawTotal)
                    .setSalesmanWithdrawTotal(salesmanWithdrawTotal)
                    .setFlowPlatformWithdrawTotal(flowPlatformWithdrawTotal);
        }
        return exchangeStatisticsVO;
    }

}
